"""
Case Type   : HTAP行列融合特性
Case Name   : 全表转换后修改数据，验证查询数据正确
Create At   : 2025/1/8
Owner       : ningyali
Description :
    1.建表插入数据，并进行行列转换
    2.修改数据
    3.开启enable_imcsscan，explain和查询
    4.关闭enable_imcsscan，explain和查询
    5.清理数据环境
Expect      :
    1.执行成功
    2.执行成功
    3.执行成功, 查询计划走imcs scan
    4.执行成功，查询计划不走imcs scan,查询结果与第三步一致
    5.执行成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro


class HTAP(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('SingleDbUser')
        self.Constant = Constant()
        self.common = Common()
        self.t_name = 't_htap_single_0140'

    def test_trans(self):
        text = '----step1:建表插入数据，并进行行列转换; expect:执行成功----'
        self.log.info(text)
        sql = f'drop table if exists {self.t_name};' \
              f'create table {self.t_name}(' \
              f'    c1 integer,' \
              f'    c2 varchar(30),' \
              f'    c3 char(30));' \
              f'insert into {self.t_name} values (' \
              f'generate_series(1, 100000),' \
              f'generate_series(1, 100000)||\'aaa\',' \
              f'generate_series(1, 100000)||\'bbb\');' \
              f'alter table {self.t_name} imcstored;'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        expect_res = r'CREATE TABLE\s+INSERT 0 100000\s+ALTER TABLE'
        self.assertRegex(sql_res, expect_res, f'执行失败:{text}')

        text = '----step2:修改数据; expect:执行成功----'
        self.log.info(text)
        sql = f'update {self.t_name} set c1=c1+1;' \
              f'\\d+'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertIn('UPDATE 100000', sql_res, f'执行失败:{text}')

        text = '----step3:开启enable_imcsscan，explain和查询; ' \
               'expect:执行成功, 查询计划走imcs scan----'
        self.log.info(text)
        sql = f'set enable_imcsscan to on;' \
              f'explain (costs false) select count(c1) from {self.t_name};' \
              f'select count(c1) from {self.t_name};'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        rows1, _ = self.common.parse_sql_query_res(sql_res)
        self.assertIn('IMCStore Scan', sql_res, f'执行失败:{text}')
        self.assertEqual('100000', rows1[-1][0], f'执行失败:{text}')

        text = '----step4:关闭enable_imcsscan，explain和查询; ' \
               'expect:执行成功，查询计划不走imcs scan,查询结果与第三步一致----'
        self.log.info(text)
        sql = f'set enable_imcsscan to off;' \
              f'explain (costs false) select count(c1) from {self.t_name};' \
              f'select count(c1) from {self.t_name};'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        rows2, _ = self.common.parse_sql_query_res(sql_res)
        self.assertNotIn('IMCStore Scan', sql_res, f'执行失败:{text}')
        self.assertEqual(rows1[-1], rows2[-1], f'执行失败:{text}')

    def tearDown(self):
        text = '----step5:清理环境;expect:执行成功----'
        self.log.info(text)
        sql = f'drop table if exists {self.t_name};'
        sql_res = self.pri_sh.execut_db_sql(
            sql, env_path=macro.SINGLE_DB_ENV_PATH)
        self.assertIn('DROP TABLE', sql_res, f'修改表失败:{text}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
